//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 5
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace UpdateItemSubject
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Create a message to update
            //
            ItemIdType itemId = EWSHelpers.CreateMessage(
                binding,
                "Old Subject");
            Console.WriteLine("ChangeKey of message before UpdateItem: " +
                itemId.ChangeKey);
            
            // Update the subject
            //
            itemId = UpdateItemSubject(binding, itemId, "New Subject");
            
            Console.WriteLine("ChangeKey of message after UpdateItem: " +
                itemId.ChangeKey);
        }

        /// <summary>
        /// Update the subject of the supplied item (Listing 5-35)
        /// </summary>
        /// <param name="binding">Binding to use for the UpdateItem call</param>
        /// <param name="itemIdToChange">ItemId of the item to update</param>
        /// <param name="newSubject">The new subject to use</param>
        /// <returns>The updated id and change key</returns>
        /// 
        public static ItemIdType UpdateItemSubject(
                               ExchangeServiceBinding binding,
                               ItemIdType itemIdToChange,
                               string newSubject)
        {
            UpdateItemType request = new UpdateItemType();
            request.ConflictResolution = ConflictResolutionType.AlwaysOverwrite;
            request.MessageDisposition = MessageDispositionType.SaveOnly;
            request.MessageDispositionSpecified = true;

            // Since we are updating an item rather than a message, we do not need 
            // to include the MessageDisposition attribute.  Just remember that if 
            // you ARE dealing with a Message or descendant, you must include
            // this attribute.  Of course, including it here wouldn't cause 
            // problems, it would just be ignored.
            //

            // Let's create our item that will hold the changed subject.  Remember, 
            // we can only set a single property.
            //
            ItemType myItem = new ItemType();
            myItem.Subject = newSubject;

            // Now, create our change description using our handy overloaded 
            // constructor
            //
            SetItemFieldType setItem = new SetItemFieldType(
                   new PathToUnindexedFieldType(UnindexedFieldURIType.itemSubject),
                   myItem);

            // The change description lives within an ItemChange
            //
            ItemChangeType itemChange = new ItemChangeType();
            itemChange.Item = itemIdToChange;
            itemChange.Updates = new ItemChangeDescriptionType[] { setItem };

            // Now set our single item change on the request
            //
            request.ItemChanges = new ItemChangeType[] { itemChange };

            UpdateItemResponseType response = binding.UpdateItem(request);

            // UpdateItem gives us back ItemInfoResponseMessageType instances.  All 
            // it gives us, however, is the id of the item.

            ItemInfoResponseMessageType responseMessage =
                       response.ResponseMessages.Items[0] as
                                       ItemInfoResponseMessageType;
            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                return responseMessage.Items.Items[0].ItemId;
            }
            else
            {
                throw new Exception("Error trying to update item:  ResponseCode: " +
                        responseMessage.ResponseCode);
            }
        }

    }
}
